layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;

    //设备表格名称
    $.get('/table/list',function (data) {
        $("#name").val('trace_table_datarecord_' + (data.count + 1));
    });
    var _companyId = $.cookie("companyId");
    $("#createBy").val(_companyId);
    //追加新的商品规格项
    var i = 0;
    $("#add-spec-item").click(function () {
        i++;
        var index = $("#rows-list .layui-input-block").length;
        var innerHtml = '<div style="margin-top: 10px;" class="layui-input-block">' +
            '                <div class="layui-input-inline">' +
            '                   <input type="text" name="tableRows['+ index +'].name" id="item-name" lay-verify="required" placeholder="字段名" autocomplete="off" class="layui-input" value="">' +
            '                </div>' +
            '                <div class="layui-form-mid">-</div>' +
            /*'                <div class="layui-input-inline">' +
            '                   <input type="text" name="tableRows['+ index +'].type" id="type-'+ index +'" placeholder="字段类型和大小" autocomplete="off" class="layui-input price" value="">' +
            '                </div>' +*/
            '                <div class="layui-input-inline">' +
            '                   <select name="tableRows['+ index +'].type" id="type-'+ index +'" lay-verify="required" lay-search>' +
            '                       <option value="">字段类型和大小</option>' +
            '                   </select>' +
            '                </div>' +
            '                <div class="layui-form-mid">-</div>' +
            '                <div class="layui-input-inline">' +
            '                   <input type="text" name="tableRows['+ index +'].nameZh" id="price" placeholder="中文名称" autocomplete="off" class="layui-input stock" value="">' +
            '                </div>' +
            '                <div class="layui-input-inline">' +
            '                   <select id="" name="tableRows['+ index +'].formType" id="formType" lay-verify="required">' +
            '                       <option value="">表单类型</option>' +
            '                       <option value="input">输入框</option>' +
            '                       <option value="textarea">文本输入框</option>' +
            '                       <option value="date">时间</option>' +
            '                       <option value="img">图片</option>' +
            '                       <option value="layedit">富文本编辑器</option>' +
            '                   </select>' +
            '                </div>' +
            /*'                <button id="del-spec-item'+ (i) +'" class="layui-btn layui-btn-danger"><i class="layui-icon layui-icon-delete"></i>删除</button>'+*/
            '            </div>';
        $("#rows-list").append(innerHtml);
        //加载字段类型
        $.get("/dictionary/list/cc855bed992a42a991a78ee76ee8e078",function (data) {
            $.each(data.data,function (i,ele) {
                $("#type-" + index).append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
            });
            form.render('select');
        });
        form.render('select');
        return false;
    });

    $.get("/dictionary/list/cc855bed992a42a991a78ee76ee8e078",function (data) {
        $.each(data.data,function (i,ele) {
            $("#type").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
        });
        form.render('select');
    });

    //绑定事件
    $("#del-spec-item").click(function () {
        $("#rows-list").children(".layui-input-block:last-child").remove();
        /*$(this).parent().remove();
        $(this).remove();*/
        return false;
    });

    //添加表格类型信息
    form.on('submit(add)', function(data){
        //判断字段名称是否重复
        var rowsArr = [];
        for (var i = 0 ; i < $("#rows-list .layui-input-block").length;i++) {
            if (contains(rowsArr,data.field['tableRows['+ i + '].name']) != -1) {
                rowsArr.push(data.field['tableRows['+ i + '].name']);
            } else {
                layer.msg("字段名相同，请检查！");
                return false;
            }
        }
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/table/add',
            data: data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

    /**
     * 判断数组中是否存在元素
     * @param arr
     * @param obj
     * @returns {number}
     */
    function contains(arr, obj) {
        if (arr.length != 0) {
            for (var i = 0;i < arr.length;i++) {
                if(arr[i] == obj) {
                    return -1;
                }
            }
        }
        return 0;
    }

});

